﻿Imports System.Data
Imports System.Xml
Public Class MonHocMo

    Dim dtProvider As New MonHocMoDAO()
    Private Sub MonHocMo_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dgvMonHocMo.DataSource = MonHocMoDAO.GetData()
        LayDSMonHocMo()
        LayDSHK()
        LayDSGV()
        LayDSMH()
    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        Dim bangMonHocMo As DataTable = MonHocMoDAO.LayCauTrucBang()
        Dim mhm As DataRow = bangMonHocMo.NewRow()

        mhm("SiSoToiDa") = Integer.Parse(txtSiSoToiDa.Text.Trim())
        mhm("SiSoHienTai") = Integer.Parse(txtSiSoToiDa.Text.Trim())

        Dim giangvien As DataRowView = cboGV.Items(cboGV.SelectedIndex)
        mhm("MaGiangVien") = giangvien("MaGiangVien")
        Dim hocky As DataRowView = cboHocKy.Items(cboHocKy.SelectedIndex)
        mhm("MaHocKy") = hocky("MaHocKy")
        Dim monhoc As DataRowView = cboMonHoc.Items(cboMonHoc.SelectedIndex)
        mhm("MaMonHoc") = monhoc("MaMonHoc")

        bangMonHocMo.Rows.Add(mhm)
        MonHocMoDAO.CapNhat(bangMonHocMo)
        LayDSMonHocMo()

        MessageBox.Show("Thêm thành công", "Thong bao", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        dgvMonHocMo.Rows.Remove(dgvMonHocMo.CurrentRow)
        Dim bangMonHocMo As DataTable = dgvMonHocMo.DataSource
        MonHocMoDAO.CapNhat(bangMonHocMo)
        LayDSMonHocMo()
        MessageBox.Show("Xóa thành công", "Thong bao", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        Dim bangMonHocMo As DataTable = dgvMonHocMo.DataSource
        Dim mhm As DataRow = bangMonHocMo.Rows(dgvMonHocMo.CurrentRow.Index)
        mhm.BeginEdit()

        mhm("SiSoToiDa") = Integer.Parse(txtSiSoToiDa.Text.Trim())
        mhm("SiSoHienTai") = Integer.Parse(txtSiSoToiDa.Text.Trim())

        Dim giangvien As DataRowView = cboGV.Items(cboGV.SelectedIndex)
        mhm("MaGiangVien") = giangvien("MaGiangVien")
        Dim hocky As DataRowView = cboHocKy.Items(cboHocKy.SelectedIndex)
        mhm("MaHocKy") = hocky("MaHocKy")
        Dim monhoc As DataRowView = cboMonHoc.Items(cboMonHoc.SelectedIndex)
        mhm("MaMonHoc") = monhoc("MaMonHoc")

        mhm.EndEdit()
        MonHocMoDAO.CapNhat(bangMonHocMo)
        LayDSMonHocMo()
        MessageBox.Show("Sửa thành công", "Thong bao", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        Dim bangMonHocMo As DataTable = MonHocMoDAO.LayCauTrucBang()
        Dim mhm As DataRow = bangMonHocMo.NewRow()
        mhm("MaMonHoc") = cboMonHoc.Text.Trim()
        mhm("SiSoToiDa") = Integer.Parse(txtSiSoToiDa.Text.Trim())
        mhm("SiSoHienTai") = Integer.Parse(txtSiSoToiDa.Text.Trim())
        mhm("MaHocKy") = Integer.Parse(cboHocKy.Text.Trim())
        mhm("MaGiangVien") = cboGV.Text.Trim()

        bangMonHocMo.Rows.Add(mhm)
        MonHocMoDAO.CapNhat(bangMonHocMo)
        LayDSMonHocMo()

        MessageBox.Show("Update thành công", "Thong bao", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        If (MessageBox.Show("Bạn có chắc chắn muốn thoát hay không?", "Thoát khỏi chương trình", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes) Then
            Me.Close()
            Global.System.Windows.Forms.Application.Exit()
        End If
    End Sub

    Private Sub dgvMonHocMo_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvMonHocMo.CellContentClick
        Dim dong As DataGridViewRow = dgvMonHocMo.CurrentRow
        If (dong Is Nothing) Then
            Exit Sub
        End If
        txtSiSoToiDa.Text = dong.Cells("SiSoToiDa").Value.ToString()
        txtSiSoHienTai.Text = dong.Cells("SiSoHienTai").Value.ToString()

        Dim viTrilop As Integer = 0
        For viTrilop = 0 To cboGV.Items.Count - 1
            Dim gv As DataRowView = cboGV.Items(viTrilop)
            If (dong.Cells("HoTen").Value.ToString() = gv(0).ToString()) Then
                Exit For
            End If
        Next
        If (viTrilop = cboGV.Items.Count - 1) Then
            viTrilop = -1
        End If
        cboGV.SelectedIndex = viTrilop

        Dim viTriHK As Integer = 0
        For viTriHK = 0 To cboHocKy.Items.Count - 1
            Dim hk As DataRowView = cboHocKy.Items(viTriHK)
            If (dong.Cells("Nam").Value.ToString() = hk(0).ToString()) Then
                Exit For
            End If
        Next
        If (viTriHK = cboHocKy.Items.Count - 1) Then
            viTriHK = -1
        End If
        cboHocKy.SelectedIndex = viTriHK

        Dim viTriMH As Integer = 0
        For viTriMH = 0 To cboMonHoc.Items.Count - 1
            Dim hk As DataRowView = cboMonHoc.Items(viTriMH)
            If (dong.Cells("TenMonHoc").Value.ToString() = hk(0).ToString()) Then
                Exit For
            End If
        Next
        If (viTriMH = cboMonHoc.Items.Count - 1) Then
            viTriMH = -1
        End If
        cboMonHoc.SelectedIndex = viTriMH
    End Sub

    Public Sub LayDSMonHocMo()
        Dim bangMonHocMo As DataTable = MonHocMoDAO.LayDSMONHOCMO()
        dgvMonHocMo.DataSource = bangMonHocMo
        dgvMonHocMo.Columns(0).Visible = True
        dgvMonHocMo.Columns(1).Visible = True
        dgvMonHocMo.Columns(2).Visible = True
        dgvMonHocMo.Columns(3).Visible = False
        dgvMonHocMo.Columns(5).Visible = False
        dgvMonHocMo.Columns(7).Visible = False

        dgvMonHocMo.Columns(1).HeaderText = "Sỉ số tối đa"
        dgvMonHocMo.Columns(2).HeaderText = "Sỉ số hiện tại"
        dgvMonHocMo.Columns(4).HeaderText = "Giáo viên lý thuyết"
        dgvMonHocMo.Columns(6).HeaderText = "Học kỳ"
        dgvMonHocMo.Columns(8).HeaderText = "Tên môn học"

        For i As Integer = 0 To dgvMonHocMo.Columns.Count - 1
            dgvMonHocMo.Columns(i).ReadOnly = True
        Next
    End Sub

    Public Sub LayDSGV()
        cboGV.DataSource = GiangVienDAO.LayDSGiangVien()
        cboGV.DisplayMember = "HoTen"
    End Sub
    Public Sub LayDSHK()
        cboHocKy.DataSource = HocKyDAO.LayDSHocKy()
        cboHocKy.DisplayMember = "Nam"
    End Sub

    Public Sub LayDSMH()
        cboMonHoc.DataSource = MonHocDAO.LayDSMonHoc()
        cboMonHoc.DisplayMember = "TenMonHoc"
    End Sub
End Class